import numpy as np
import matplotlib.pyplot as plt

def model(x,theta):
    return x.dot(theta)

def costfunc(h,y):
    return 0.5*np.mean((h-y)**2)

def grad(x,y,alpha=0.02,iter0=8000):
    m,n=x.shape
    theta=np.zeros(n)
    J=np.zeros(iter0)
    for i in range(iter0):
        h=model(x,theta)
        J[i]=costfunc(h,y)
        dt=1/m*x.T.dot(h-y)
        theta-=alpha*dt
    return J,h,theta

if __name__ == '__main__':
    data=np.loadtxt('data.txt',delimiter=',')
    x=data[:,:-1]
    y=data[:,-1]
    X=np.c_[np.ones(len(x)),x]

    num = int(0.7 * len(x))
    train_x, test_x = X[:num,:], X[num:,:]
    train_y, test_y = y[:num], y[num:]

    J,h,theta=grad(train_x,train_y)

    test_h=model(test_x,theta)
    # print(train_x)
    plt.scatter(train_x[:,1],train_y)
    plt.show()
    plt.scatter(test_x[:,1],test_h)
    plt.show()